﻿using System;
using System.Windows.Forms;
using SdmxMl.Structure;

namespace SdmxMl.UiEditors.UC
{
    /// <summary>
    /// Edit Control for SDMX 2.1 added properties in Report meta item description
    /// Like: occurences, isPresentational
    /// </summary>
    public partial class UcMetaOccurence : UserControl
    {
        #region Internal Members

        private MetaAttributeOccurences metaOcc;
        private bool editEnabled;
        private bool inBind;

        #endregion

        #region C'tor

        public UcMetaOccurence()
        {
            InitializeComponent();

            // Fill combo for patterns of occurences
            comboBoxOccurence.Items.Clear();
            comboBoxOccurence.Items.AddRange(MetaAttributeOccurences.OccurencePatternStr);

           // ((Control)this.numericUpDownMin).Text = string.Empty;
        }
        #endregion

        #region Binding

        public void BindMetaOccurences(MetaAttributeOccurences mo, bool isFinal)
        {
            metaOcc = mo;
            editEnabled = isFinal == false;

            inBind = true;

            SetMinMaxControls(metaOcc);
            comboBoxOccurence.SelectedItem = metaOcc.ToString();
            comboBoxOccurence.Enabled = editEnabled;
            checkBoxIsPresentational.Checked = metaOcc.IsPresentational;
            checkBoxIsPresentational.Enabled = editEnabled;
            
            inBind = false;
        }

        private void SetMinMaxControls(MetaAttributeOccurences mo)
        {
            MetaAttributeOccurences.eTypeOccurs met = mo.OccurencesPattern;

            textBoxMandatoryOptional.Text = mo.UsageStatus.ToString();

            switch (met)
            {
                case MetaAttributeOccurences.eTypeOccurs.T_0_1 :
                    numericUpDownMin.Enabled = false;
                    numericUpDownMin.Minimum = 0;
                    numericUpDownMin.Value = 0;

                    numericUpDownMax.Enabled = false;
                    numericUpDownMax.Minimum = 1;
                    numericUpDownMax.Value = 1;
                    break;

                case MetaAttributeOccurences.eTypeOccurs.T_0_n:
                    numericUpDownMin.Enabled = false;
                    numericUpDownMin.Minimum = 0;
                    numericUpDownMin.Value = 0;

                    numericUpDownMax.Enabled = editEnabled;
                    numericUpDownMax.Minimum = 2;
                    if (mo.MaxUnspecified)
                        ((Control)this.numericUpDownMax).Text = string.Empty;
                    else
                    {
                        numericUpDownMax.Value = mo.MaxOccurs;
                        ((Control)this.numericUpDownMax).Text = mo.MaxOccurs.ToString();
                    }
                    break;

                case MetaAttributeOccurences.eTypeOccurs.T_1_1:
                    numericUpDownMin.Enabled = false;
                    numericUpDownMin.Minimum = 1;
                    numericUpDownMin.Value = 1;
                    numericUpDownMax.Minimum = 1;
                    numericUpDownMax.Enabled = false;
                    numericUpDownMax.Value = 1;
                    break;

                case MetaAttributeOccurences.eTypeOccurs.T_1_n:
                    numericUpDownMin.Enabled = false;
                    numericUpDownMin.Minimum = 1;
                    numericUpDownMin.Value = 1;

                    numericUpDownMax.Enabled = editEnabled;
                    numericUpDownMax.Minimum = 2;
                    if (mo.MaxUnspecified)
                        ((Control)this.numericUpDownMax).Text = string.Empty;
                    else
                    {
                        numericUpDownMax.Value = mo.MaxOccurs;
                        ((Control)this.numericUpDownMax).Text = mo.MaxOccurs.ToString();
                    }
                    break;

                case MetaAttributeOccurences.eTypeOccurs.T_m_n:
                    numericUpDownMin.Enabled = editEnabled;
                    numericUpDownMin.Minimum = 2;
                    if (mo.MinUnspecified)
                        ((Control)this.numericUpDownMin).Text = string.Empty;
                    else
                    {
                        numericUpDownMin.Value = mo.MinOccurs;
                        ((Control)this.numericUpDownMin).Text = mo.MinOccurs.ToString();
                    }

                    numericUpDownMax.Minimum = 2;
                    numericUpDownMax.Enabled = editEnabled;
                    if (mo.MaxUnspecified)
                        ((Control)this.numericUpDownMax).Text = string.Empty;
                    else
                    {
                        numericUpDownMax.Value = mo.MaxOccurs;
                        ((Control)this.numericUpDownMax).Text = mo.MaxOccurs.ToString();
                    }
                        
                    break;
            }
        }
        #endregion

        #region Public Methods

        /// <summary> Method for updating occurences counts</summary>
        public void UpdateOccurenceValues()
        {
                metaOcc.UpdatePattern((MetaAttributeOccurences.eTypeOccurs)comboBoxOccurence.SelectedIndex,
                     ((Control)this.numericUpDownMin).Text, ((Control)this.numericUpDownMax).Text);
        }
        #endregion

        #region Controls events

        private void comboBoxOccurence_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!inBind)
            {
                metaOcc.UpdatePattern((MetaAttributeOccurences.eTypeOccurs)comboBoxOccurence.SelectedIndex);
                SetMinMaxControls(metaOcc);
            }
        }

        private void checkBoxIsPresentational_CheckedChanged(object sender, EventArgs e)
        {
            if (!inBind)
                metaOcc.IsPresentational = checkBoxIsPresentational.Checked; ;

        }
        #endregion
    }
}
